<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Register and add callback function</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="class.gearmanworker.html">« GearmanWorker</a></li>
      <li style="float: right;"><a href="gearmanworker.addoptions.html">GearmanWorker::addOptions »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.gearmanworker.html">GearmanWorker</a></li>
    <li>Register and add callback function</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="gearmanworker.addfunction" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">GearmanWorker::addFunction</h1>
  <p class="verinfo">(PECL gearman &gt;= 0.5.0)</p><p class="refpurpose"><span class="refname">GearmanWorker::addFunction</span> &mdash; <span class="dc-title">Register and add callback function</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-gearmanworker.addfunction-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>GearmanWorker::addFunction</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$function_name</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$function</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter reference">&$context</code><span class="initializer"> = ?</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$timeout</code><span class="initializer"> = ?</span></span><br>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   Registers a function name with the job server and specifies a callback corresponding to that
   function.  Optionally specify extra application context data to be used when the callback is
   called and a timeout.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-gearmanworker.addfunction-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">function_name</code></dt>

     <dd>

      <p class="para">
       The name of a function to register with the job server
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">function</code></dt>

     <dd>

      <p class="para">
       A callback that gets called when a job for the registered function name is submitted
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">context</code></dt>

     <dd>

      <p class="para">
       A reference to arbitrary application context data that can be modified by the worker function
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">timeout</code></dt>

     <dd>

      <p class="para">
       An interval of time in seconds
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-gearmanworker.addfunction-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-gearmanworker.addfunction-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4316">
    <p><strong>示例 #1 Simple worker making use of extra application context data</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">#&nbsp;get&nbsp;a&nbsp;gearman&nbsp;worker<br /></span><span style="color: #0000BB">$worker</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">GearmanWorker</span><span style="color: #007700">();&nbsp;<br /><br /></span><span style="color: #FF8000">#&nbsp;add&nbsp;the&nbsp;default&nbsp;server&nbsp;(localhost)<br /></span><span style="color: #0000BB">$worker</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addServer</span><span style="color: #007700">();&nbsp;<br /><br /></span><span style="color: #FF8000">#&nbsp;define&nbsp;a&nbsp;variable&nbsp;to&nbsp;hold&nbsp;application&nbsp;data<br /></span><span style="color: #0000BB">$count</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;<br /><br /></span><span style="color: #FF8000">#&nbsp;add&nbsp;the&nbsp;"reverse"&nbsp;function<br /></span><span style="color: #0000BB">$worker</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addFunction</span><span style="color: #007700">(</span><span style="color: #DD0000">"reverse"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"reverse_cb"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$count</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">#&nbsp;start&nbsp;the&nbsp;worker<br /></span><span style="color: #007700">while&nbsp;(</span><span style="color: #0000BB">$worker</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">work</span><span style="color: #007700">());<br /><br />function&nbsp;</span><span style="color: #0000BB">reverse_cb</span><span style="color: #007700">(</span><span style="color: #0000BB">$job</span><span style="color: #007700">,&nbsp;&amp;</span><span style="color: #0000BB">$count</span><span style="color: #007700">)&nbsp;<br />{&nbsp;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$count</span><span style="color: #007700">++;&nbsp;<br />&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$count</span><span style="color: #DD0000">:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">strrev</span><span style="color: #007700">(</span><span style="color: #0000BB">$job</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">workload</span><span style="color: #007700">());&nbsp;<br />}&nbsp;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     Running a client that submits two jobs for the reverse function would have
     output similar to the following:
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
1: olleh
2: dlrow
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-gearmanworker.addfunction-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="gearmanclient.do.html" class="methodname" rel="rdfs-seeAlso">GearmanClient::do()</a> - Run a single task and return a result [deprecated]</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>